package com.jbm.jbmsupplier.activity.register.mode

import com.jbm.enterapplication.InterfaceMa
import com.jbm.jbmsupplier.activity.rxjavanet.ModeRxjavaNet
import com.jbm.onehome.iface.Login
import rx.Observable
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

/**
 * Created by 家帮忙 on 2017/8/22.
 */
class Regiter_Mode_User : Reginster_UsertInter <String>{
    override fun GainRegisterIntergace(phont: String, validate: String, pwds: String, pwdszc: String,loginid:String, unit: String,oclicklist: InterfaceMa<String>) {//注册为管理员

        Observable.create(Observable.OnSubscribe <Login> {
            register ->
            register.onNext(ModeRxjavaNet.getInstantMode().getOhInter().registManager(Integer.parseInt(loginid),phont,pwds,pwdszc,validate,unit) )
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<Login> {
                    override fun onNext(t: Login?) {
                        if (t?.m_code == 200) {
                            //搭桥成功
                            oclicklist.onSuccess(t.login_id.toString(),true)
                        } else if (t?.m_info == "time out") {
                            oclicklist.setError(t.m_info,false)
                            //搭桥超时
                        } else if (t?.m_code == 500) {
                            oclicklist.setError(t.m_info,false)
                            //搭桥失败
                        } else {
                            oclicklist.setError(t!!.m_info,false)
                        }
                    }
                    override fun onError(e: Throwable?) {


                    }

                    override fun onCompleted() {



                    }
                })

    }

    override fun GainValidateCode(validate: String, oclicklist: InterfaceMa<String>) {//获取验证码

            Observable.create(Observable.OnSubscribe <Login> {
                register ->
                register.onNext(ModeRxjavaNet.getInstantMode().getOhInter() .RegistCustomer(0, validate, "", "", "", false,""))
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(object : Observer<Login> {
                        override fun onNext(t: Login?) {
                            if (t?.m_code==200){
                                oclicklist.onSuccess(t.login_id.toString(),true)
                            } else if (t?.m_info == "time out") {
                                oclicklist.setError(t.m_info,false)
                                //手机号获取验证码超时

                            } else if (t?.m_code == 500) {
//                        手机号获取验证码失败
                                oclicklist.setError(t.m_info,false)
                            } else {
                                //手机号获取验证码失败
                                oclicklist.setError(t!!.m_info,false)
                            }
                        }
                        override fun onError(e: Throwable?) {
                        }

                        override fun onCompleted() {
                        }
                    })
        }


}